package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.framework.aspectj.lang.annotation.Ide;
import com.ruoyi.project.system.domain.vo.GoodsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysGoods;
import com.ruoyi.project.system.service.ISysGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

import javax.validation.constraints.NotNull;

/**
 * 【请填写功能名称】Controller
 *
 * @author theme
 * @date 2020-06-23
 */
@Api(description = "商品Controller")
@RestController
@RequestMapping("/system/goods")
public class SysGoodsController extends BaseController {
    @Autowired
    private ISysGoodsService sysGoodsService;

    /**
     * 查询所以商品列表
     */
    @ApiOperation("查询所以商品列表")
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysGoods sysGoods) {
        startPage();
        sysGoods.setDelFlag("0");
        List<SysGoods> list = sysGoodsService.selectSysGoodsList(sysGoods);
        return getDataTable(list);
    }

    /**
     * 新增商品
     */
    @ApiOperation("新增商品")
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @Ide
    @PostMapping("/create")
    public AjaxResult create(@RequestBody GoodsVo goodsVo) {
        AjaxResult ajaxResult = sysGoodsService.create(goodsVo);
        return ajaxResult;
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "删除商品", businessType = BusinessType.DELETE)
    @Ide
    @DeleteMapping("/{goodsIds}")
    public AjaxResult delete(@PathVariable Long goodsIds) {
        SysGoods goods = new SysGoods();
        goods.setGoodsId(goodsIds);
        goods.setDelFlag("1");
        return toAjax(sysGoodsService.updateSysGoods(goods));
    }


    /**
     * 商品修改
     */
    @ApiOperation("商品修改")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @Ide
    @PutMapping("/update")
    public AjaxResult update(@RequestBody GoodsVo goodsVo) {
        return sysGoodsService.update(goodsVo);
    }

    /**
     * 查询单个商品详情
     */
    @ApiOperation("查询单个商品详情")
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{goodsId}")
    public AjaxResult queryOne(@PathVariable Long goodsId) {
        return AjaxResult.success(sysGoodsService.queryOne(goodsId));
    }

    /**
     * 商品上下架
     */
    @ApiOperation("商品上下架")
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "商品上下架", businessType = BusinessType.UPDATE)
    @Ide
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody SysGoods sysGoods) {
        return toAjax(sysGoodsService.updateSysGoods(sysGoods));
    }

}
